<template>
<div>
    <loginLogo/>
    <div class="signUp">
        <div class="signUp-head">
            <span>帐号登录</span>
        </div>
        <el-form :model="loginForm" ref="loginForm" label-width="70px" class="demo-ruleForm" :rules="rules">
            <el-form-item prop="email" label="邮箱">
                <el-input v-model="loginForm.email" placeholder="邮箱"></el-input>
            </el-form-item>
            <el-form-item prop="password" label="密码">
                <el-input type="password" v-model="loginForm.password" placeholder="密码"></el-input>
            </el-form-item>
            
            <div class="login-btn">
                <el-button @click="goSignUp">注册</el-button>
                <el-button type="primary" @click="handleLoginIn">登录</el-button>
            </div>
        </el-form>
    </div>
</div>
</template>
<script>
import loginLogo from '../components/LoginLogo'
import {mixin} from '../mixins'
export default {
    name: 'login-in',
    mixins: [mixin],
    components: {
        loginLogo
    },
    data() {
        return {
            loginForm: {
                email: '',       //邮箱
                password: '',       //密码
            },
            rules: {
                email: [
                    { required: true, trigger: 'blur',message: '请输入邮箱' },
                    {
            pattern:
              /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/,
            message: "邮箱格式不正确",
            trigger: "blur",
          },

                ],
                password: [
                    { required: true, trigger: 'blur',message: '请输入密码' }
                ]
            }
        }    
    },
    mounted() {
        this.changeIndex('登录');
    },
    methods:{
        handleLoginIn(){
            const _this = this;
           _this.$refs.loginForm.validate((valid) => {
          if (valid) {
            _this.postRequest("/user/login/status",_this.loginForm).then((res) => {
            if(res.data.code == 1){
                _this.$notify({
                title: res.data.msg,
                type: 'success'
            });
                this.$router.replace({path: '/home'});
                this.$store.commit('setActiveName','首页');
                _this.$store.commit('setLoginIn',true);
                        _this.$store.commit('setUserId',res.data.userid);
                        _this.$store.commit('setUsername',res.data.username);
                        _this.$store.commit('setAvator',res.data.useravator);                
            }else{
                _this.$notify({
                title: res.data.msg,
                type: 'error'
            });
            }
        });
          } else {
            _this.$notify({
                title: '信息填写有误',
                type: 'error'
            });
            return false;
          }
        });
        },
        goSignUp(){
            this.changeIndex('注册');
            this.$router.push({path: '/sign-up'});
        },
        changeIndex(value){
            this.$store.commit('setActiveName',value);
        }
    }
}
</script>

<style scoped>
.signUp {
  position: absolute;
  top: 90px;
  background-color: white;
  border-radius: 10px;
  width: 350px;
  margin-left: 900px;
  margin-top: 200px;
  padding: 30px 30px;
  
}
.signUp-head {
    text-align: center;
    margin-bottom: 10px;
    font-size: 20px;
    font-weight: 600;
  }

.login-btn {
    display: flex;
    justify-content: flex-start;
    align-items: stretch;
    flex-direction: row;
    flex-wrap: nowrap;
    
}
button {
      display: block;
      width: 50%;
    }
</style>